﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using PeppeDotNet.SP2010.Html5Fields.Number;
using System.Globalization;

namespace PeppeDotNet.SP2010.Html5Fields.CONTROLTEMPLATES
{
    public partial class Html5NumberInputFieldEditor : UserControl, IFieldEditor
    {      
        public bool DisplayAsNewSection
        {
            get { return true; }
        }

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.MaintainScrollPositionOnPostBack = true; //required to keep section within view
            if (!this.IsViewStateEnabled) { this.EnableViewState = true; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void InitializeWithField(SPField field)
        {
            EnsureChildControls();

            Html5NumberInputField currentField = field as Html5NumberInputField;
            if (currentField != null && !IsPostBack)
            {
                if (currentField.MinimumValue != double.MinValue)
                    txtMin.Value = String.Format(CultureInfo.CurrentUICulture, "{0}", currentField.MinimumValue);
                if (currentField.MaximumValue != double.MaxValue)
                    txtMax.Value = String.Format(CultureInfo.CurrentUICulture, "{0}", currentField.MaximumValue);
                if (currentField.Step != 0)
                    txtStep.Value = String.Format(CultureInfo.CurrentUICulture, "{0}", currentField.Step);

                chBoxPercentage.Checked = currentField.ShowAsPercentage;
                int decimalPlaces = (int)currentField.DisplayFormat;
                ListItem decimalPlacesItem = ddlDecimalPlaces.Items.FindByValue((decimalPlaces == -1) ? string.Empty : decimalPlaces.ToString());
                if (decimalPlacesItem != null) decimalPlacesItem.Selected = true;
            }
        }

        public void OnSaveChange(SPField field, bool isNewField)
        {
            EnsureChildControls();
            Html5NumberInputField currentField = field as Html5NumberInputField;
            if (currentField != null)
            {
                if (!String.IsNullOrEmpty(txtMax.Value)) currentField.MaximumValue = double.Parse(txtMax.Value);
                if (!String.IsNullOrEmpty(txtMin.Value)) currentField.MinimumValue = double.Parse(txtMin.Value);
                if (!String.IsNullOrEmpty(txtStep.Value)) currentField.Step = double.Parse(txtStep.Value);
                currentField.ShowAsPercentage = chBoxPercentage.Checked;
                currentField.DisplayFormat = (String.IsNullOrEmpty(ddlDecimalPlaces.SelectedValue) ? SPNumberFormatTypes.Automatic : (SPNumberFormatTypes)Int32.Parse(ddlDecimalPlaces.SelectedValue));
            }
        }
    }
}
